let mongoose = require("mongoose");
const { tabsModel } = require("../db/model/tabs");
let getOne = async (navsId) => {
  console.log(navsId);
  let tabsModel = mongoose.model("tabs");
  const data = await tabsModel.find({ navsId }).exec();
  return data;
};
let getOneTabs = async (_id) => {
  console.log(_id);
  let tabsModel = mongoose.model("tabs");
  const data = await tabsModel.findOne({ _id }).exec();
  console.log(data);
  return data;
};
const changeTab = async ({ _id, name }) => {
  console.log({ _id, name });
  const tabsModel = mongoose.model("tabs");
  await tabsModel.findOneAndUpdate({ _id }, { name });
  const data = true;
  return data;
};
const remove=async ({_id})=>{
  console.log(_id,"api");
  const tabsModel = mongoose.model("tabs");
  return await tabsModel.remove({_id});
}
//获取商品类型
const getTabs = async (navsId) => {
  let newArr = [];
  const tabsModel = mongoose.model("tabs");
  const data = await tabsModel.find();
  for (let i = 0; i < data.length; i++) {
    if (data[i].navsId == navsId) {
      newArr.push(data[i]);
    }
  }
  return newArr;
};
//新增商品类型
const addTab = async ({ name, navsId }) => {
  const tabsModel = mongoose.model("tabs");
  return await tabsModel.create({ name, navsId });
};
module.exports = { getTabs,addTab,getOne,getOneTabs,changeTab,remove };
